package com.sneakxy.mybase4j.platform.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sneakxy.mybase4j.base.bean.ValidAdd;
import com.sneakxy.mybase4j.base.bean.ValidEdit;
import com.sneakxy.mybase4j.base.controller.BaseController;
import com.sneakxy.mybase4j.platform.beans.Permission;
import com.sneakxy.mybase4j.platform.service.IPermissionService;
import com.sneakxy.mybase4j.platform.service.IRolePermService;
import com.sneakxy.mybase4j.platform.service.IRouterService;
import com.sneakxy.mybase4j.util.BeanHelper;
import com.sneakxy.mybase4j.util.Response;

/**
 * 权限管理
 * @author 潜行的青衣
 * @see
 */
@RestController
@RequestMapping("/api/permission/")
public class PermissionController extends BaseController {

	@Resource
	private IPermissionService permissionService;
	
	@Resource
	private IRolePermService rolePermService;
	
	@Resource
	private IRouterService routerService;
	
	@RequiresPermissions("sys:perm:manager")
	@RequestMapping("tree")
	public Response tree() {
		List<Permission> list = this.permissionService.findAllByParentId(true);
		return new Response(list);
	}
	
	@RequiresPermissions("sys:perm:manager:add")
	@RequestMapping("add")
	public Response add(@Validated(ValidAdd.class) Permission perm, BindingResult result) {
		if(result.hasErrors()) {
			return this.getFieldErrorsResponse(result);
		}
		if(!this.validate(perm.getId())) {
			return new Response(false, 1);
		}
		//去空格
		BeanHelper.trimToEmpty(perm);
		perm.setParentId(StringUtils.trimToNull(perm.getParentId()));
		
		perm.setNew(true);
		perm = this.permissionService.save(perm);
		return new Response(true, perm);
	}
	
	@RequiresPermissions("sys:perm:manager:add")
	@RequestMapping("validate")
	public Boolean validate(String id) {
		Permission old = this.permissionService.findOne(id);
		return old == null;
	}
	
	@RequiresPermissions("sys:perm:manager:edit")
	@RequestMapping("edit")
	public Response edit(@Validated(ValidEdit.class) Permission perm, BindingResult result) {
		if(result.hasErrors()) {
			return this.getFieldErrorsResponse(result);
		}
		Permission old = this.permissionService.findOne(perm.getId());
		//去空格
		BeanHelper.trimToEmpty(perm);
		
		perm.setNew(false);
		perm.setParentId(old.getParentId());
		perm = this.permissionService.save(perm);
		return new Response(true, perm);
	}
	
	@RequiresPermissions("sys:perm:manager:remove")
	@RequestMapping("remove")
	public Response remove(String id) {
		String permId = StringUtils.trimToEmpty(id);
		if(StringUtils.isNotBlank(permId) && this.permissionService.countByParentId(id) == 0) {
			this.rolePermService.deleteByPermId(permId);
			this.routerService.updatePermIdToNull(permId);
			this.permissionService.delete(permId);
			return new Response(true);
		}
		return new Response(false, 1);
	}
	
}
